
//--------------------------------------------------------------------//
//     此代码由T4模板自动生成										  //
//	   生成时间 2020-06-22 10:34:56 	                              //
//	   描述：编号规则表
//--------------------------------------------------------------------//
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.Collections.Generic;
using System.IO;
using System.Data;
using RayD.App.SystemModule;
using RayD.Repository.Entites.SystemModule;
using RayD.Core;
using Microsoft.AspNetCore.Authorization;
using RayD.Repository;

namespace RayD.WebApi.Controllers.Admin
{
    public class CodeRuleController : BaseAdminController
    {
        #region 构造函数注入
        private CodeRuleApp _CodeRule;
        public CodeRuleController(CodeRuleApp CodeRule)
        {
            this._CodeRule = CodeRule;
        }
        #endregion

        #region 请求参数
        /// <summary>
        /// 保存实体请求参数
        /// </summary>
        public class SaveFormRequest
        {
            /// <summary>
            /// 规则实体
            /// </summary>
            public CodeRuleEntity codeRuleEntity { get; set; }
            /// <summary>
            /// 规则格式集
            /// </summary>
            public List<CodeRuleFormatEntity> formatList { get; set; }
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="queryjson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<IEnumerable<CodeRuleEntity>> GetList(string queryjson)
        {
            var list = _CodeRule.GetList(queryjson);
            return Success(list);
        }

        /// <summary>
        /// 获取规则格式列表数据
        /// </summary>
        /// <param name="id">编码规则主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<List<CodeRuleFormatEntity>> GetFormatList(string id)
        {
            var list = _CodeRule.GetFormatList(id);
            return Success(list);
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体(新增,编辑)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut, Logging("编码规则保存")]
        public ResParameter SaveForm(SaveFormRequest request)
        {
            var (isSuccess, msg) = _CodeRule.SaveForm(request.codeRuleEntity, request.formatList);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns></returns>
        [HttpPut, Logging("编码规则删除")]
        public ResParameter DeleteForm(CodeRuleEntity request)
        {
            var (isSuccess, msg) = _CodeRule.DeleteForm(request);
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }

        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter ValidateExist(string id, string field, string value)
        {
            var res = _CodeRule.ValidateExist(id, field, value);
            if (!res)
            {
                return Success("校验通过");
            }
            else
            {
                return Fail("值已存在，校验不通过！");
            }
        }
        #endregion

        [HttpGet]
        public ResParameter GetTest(string enCode)
        {
            return Success(_CodeRule.GetCode(enCode));
        }
    }
}
